import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtContentResourceApi } from '#/api/cmt/content/resource';

import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'resourceType',
      label: '资源类型',
      rules: 'required',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: [
          { label: '文档', value: 1 },
          { label: '图片', value: 2 },
          { label: '视频', value: 3 },
          { label: '音频', value: 4 },
          { label: '其他', value: 5 },
        ],
      },
      defaultValue: 1,
    },
    {
      fieldName: 'name',
      label: '资源名称',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入资源名称',
      },
    },
    {
      fieldName: 'category',
      label: '分类',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入分类',
      },
    },
    {
      fieldName: 'fileType',
      label: '文件类型',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '例如：pdf, jpg, mp4等',
      },
    },
    {
      fieldName: 'filePath',
      label: '文件路径',
      rules: 'required',
      component: 'FileUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请上传文件',
      },
    },
    {
      fieldName: 'fileSize',
      label: '文件大小',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '文件大小（字节）',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'tags',
      label: '标签',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入标签，多个标签用逗号分隔',
      },
    },
    {
      fieldName: 'description',
      label: '资源描述',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入资源描述',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '资源名称',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入资源名称',
      },
    },
    {
      fieldName: 'resourceType',
      label: '资源类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '文档', value: 1 },
          { label: '图片', value: 2 },
          { label: '视频', value: 3 },
          { label: '音频', value: 4 },
          { label: '其他', value: 5 },
        ],
        placeholder: '请选择资源类型',
      },
    },
    {
      fieldName: 'category',
      label: '分类',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入分类',
      },
    },
    {
      fieldName: 'fileType',
      label: '文件类型',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入文件类型',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtContentResourceApi.Resource>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '资源ID',
      minWidth: 100,
    },
    {
      field: 'name',
      title: '资源名称',
      minWidth: 200,
    },
    {
      field: 'resourceType',
      title: '资源类型',
      minWidth: 100,
      slots: { default: 'resourceType' },
    },
    {
      field: 'category',
      title: '分类',
      minWidth: 120,
    },
    {
      field: 'fileType',
      title: '文件类型',
      minWidth: 100,
    },
    {
      field: 'filePath',
      title: '文件路径',
      minWidth: 200,
    },
    {
      field: 'fileSize',
      title: '文件大小',
      minWidth: 120,
      slots: { default: 'fileSize' },
    },
    {
      field: 'downloadCount',
      title: '下载次数',
      minWidth: 100,
    },
    {
      field: 'viewCount',
      title: '浏览次数',
      minWidth: 100,
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 80,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
